﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ZooKeeperNet;

namespace DistributedLock
{
    public class ZKClient
    {
        static object _lock = new object();

        static string server = "192.168.1.232:2184";

        private static ZooKeeper _client;
        public static ZooKeeper Client
        {
            get
            {
                try
                {
                    if (_client == null || _client.State == ZooKeeper.States.CLOSED || _client.State != ZooKeeper.States.CONNECTED)
                    {
                        lock (_lock)
                        {
                            if (_client == null || _client.State == ZooKeeper.States.CLOSED || _client.State != ZooKeeper.States.CONNECTED)
                            {
                                _client = new ZooKeeper(server, TimeSpan.FromMilliseconds(-1), null);
                            }
                        }
                    }
                    return _client;
                }
                catch (Exception)
                {
                    return ZKClient.Client;
                }
            }
        }
    }
}
